package org.spark.java.transformation;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.VoidFunction;

import java.util.Arrays;
import java.util.List;

/**
 * @author: mdf
 * @Date: 2020/1/16 19:36
 * @Description:
 */
public class Filter {
    public static void main(String[] args) {
        SparkConf sparkConf = new SparkConf();
        //设置运行环境
        sparkConf.setMaster("local").setAppName("filter");
        JavaSparkContext context = new JavaSparkContext(sparkConf);
        //模拟数据
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
        JavaRDD<Integer> listRdd = context.parallelize(list);
        JavaRDD<Integer> map = listRdd.filter(new Function<Integer, Boolean>() {
            public Boolean call(Integer i) throws Exception {
                return i % 2 == 0;
            }
        });
        map.foreach(new VoidFunction<Integer>() {
            public void call(Integer i) throws Exception {
                System.out.println(i);
            }
        });
    }
}
